package com.quanyan.ic.vendingMachine.dao.impl;

import com.alibaba.fastjson.JSON;
import com.quanyan.ic.common.paging.Limit;
import com.quanyan.ic.vendingMachine.dao.VmReplenishmentRecordDAO;
import com.quanyan.ic.vendingMachine.http.request.ReqRsptRecordCondition;
import com.quanyan.ic.vendingMachine.mapper.VmReplenishmentRecordEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.VmReplenishmentRecordEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmReplenishmentRecordEntityExample;
import com.quanyan.ic.vendingMachine.model.GridInfo;
import com.quanyan.ic.vendingMachine.model.ReplenishmentRecord;
import com.quanyan.ic.vendingMachine.model.RsptGoodsSummary;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by zhuo.shi on 2017/8/1.
 */
@Repository
public class VmReplenishmentRecordDAOImpl implements VmReplenishmentRecordDAO {

    @Autowired
    private VmReplenishmentRecordEntityMapper vmReplenishmentRecordEntityMapper;

    @Override
    public List<ReplenishmentRecord> queryRsptRecordList(ReqRsptRecordCondition condition, Limit limit) {
        VmReplenishmentRecordEntityExample example = new VmReplenishmentRecordEntityExample();
        VmReplenishmentRecordEntityExample.Criteria criteria = example.createCriteria();
        buildCriteria(criteria, condition);
        example.setOrderByClause(" op_time DESC");
        example.setLimit(limit);
        List<VmReplenishmentRecordEntity> vmReplenishmentRecordEntities = vmReplenishmentRecordEntityMapper.selectByExample(example);
        if (vmReplenishmentRecordEntities == null || vmReplenishmentRecordEntities.size() <= 0) {
            return null;
        }
        List<ReplenishmentRecord> resultList = new ArrayList<>();
        for (VmReplenishmentRecordEntity entity : vmReplenishmentRecordEntities) {
            resultList.add(toModel(entity));
        }
        return resultList;
    }

    private void buildCriteria(VmReplenishmentRecordEntityExample.Criteria criteria, ReqRsptRecordCondition condition) {
        if (condition.getPlaceId() != null) {
            criteria.andPlaceIdEqualTo(condition.getPlaceId());
        }
        if (!StringUtils.isEmpty(condition.getVmNumber())) {
            criteria.andVmNumberEqualTo(condition.getVmNumber());
        }
        if (!StringUtils.isEmpty(condition.getOpUser())) {
            criteria.andOpUserEqualTo(condition.getOpUser());
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (!org.apache.commons.lang3.StringUtils.isEmpty(condition.getStartTime())) {
                criteria.andOpTimeGreaterThanOrEqualTo(sdf.parse(condition.getStartTime()));
            }
            if (!org.apache.commons.lang3.StringUtils.isEmpty(condition.getEndTime())) {
                criteria.andOpTimeLessThanOrEqualTo(sdf.parse(condition.getEndTime()));
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public long countRsptRecordList(ReqRsptRecordCondition condition) {
        VmReplenishmentRecordEntityExample example = new VmReplenishmentRecordEntityExample();
        VmReplenishmentRecordEntityExample.Criteria criteria = example.createCriteria();
        buildCriteria(criteria, condition);
        return vmReplenishmentRecordEntityMapper.countByExample(example);
    }

    @Override
    public void insert(ReplenishmentRecord record) {
        vmReplenishmentRecordEntityMapper.insertSelective(toEntity(record));
    }

    private ReplenishmentRecord toModel(VmReplenishmentRecordEntity entity) {
        ReplenishmentRecord model = new ReplenishmentRecord();
        BeanUtils.copyProperties(entity, model);
        try {
            model.setSnapshoot_1(JSON.parseArray(entity.getSnapshootBefore(), GridInfo.class));
            model.setSnapshoot_2(JSON.parseArray(entity.getSnapshootAfter(), GridInfo.class));
            model.setSummaryList(JSON.parseArray(entity.getSummary(), RsptGoodsSummary.class));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return model;
    }

    private VmReplenishmentRecordEntity toEntity(ReplenishmentRecord model) {
        VmReplenishmentRecordEntity entity = new VmReplenishmentRecordEntity();
        BeanUtils.copyProperties(model, entity);
        entity.setSnapshootBefore(JSON.toJSONString(model.getSnapshoot_1()));
        entity.setSnapshootAfter(JSON.toJSONString(model.getSnapshoot_2()));
        entity.setSummary(JSON.toJSONString(model.getSummaryList()));
        return entity;
    }
}
